import type { worker } from "../alchemy.run.ts";

export default {
  async fetch(request: Request, env: typeof worker.Env) {
    const dynamicWorker = env.LOADER.get(
      `dynamic-${Math.random()}`,
      async () => {
        return {
          compatibilityDate: "2025-06-01",
          mainModule: "index.js",
          modules: {
            "index.js": `
              export default {
                async fetch(request) {
                  return new Response('Hello! This response was generated by a dynamically loaded worker.');
                }
              }
            `,
          },
        };
      },
    );

    const entrypoint = dynamicWorker.getEntrypoint();

    return entrypoint.fetch(new URL(request.url));
  },
};
